package com.sddz.pacs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.entity.SysUserRole;
import com.sddz.pacs.mapper.SysUserMapper;
import com.sddz.pacs.service.SysUserRoleService;
import com.sddz.pacs.service.SysUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sddz.pacs.util.ShiroUtils;
import com.sddz.pacs.util.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 系统用户信息表 服务实现类
 * </p>
 *
 * @author 时代电子
 * @since 2018-09-25
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageInfo<SysUser> selectListByCondition(PageModelParams params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        //获取当前登陆场对象
        SysUser sysUser= ShiroUtils.getUserEntity();
        if (sysUser != null){
            if (sysUser.getLevels()==0){
                if (params.getCondition()!=null){
                    if (StringUtils.isNotEmpty(params.getCondition().get("status"))){
                        ew.eq("STATUS",params.getCondition().get("status"));
                    }

                    if (!StringUtils.isEmpty(params.getCondition().get("name"))){
                        ew.like("USER_NAME", params.getCondition().get("name").toString());
                    }
                }
            }else{
                if(sysUser.getLevels() ==1 && sysUser.getState().equals("0")){
                    if (params.getCondition()!=null){
                        if (StringUtils.isNotEmpty(params.getCondition().get("status"))){
                            ew.eq("STATUS",params.getCondition().get("status"));
                        }
                        if (!StringUtils.isEmpty(params.getCondition().get("name"))){
                            ew.like("USER_NAME", params.getCondition().get("name").toString());
                        }
                        if (sysUser.getState().equals("0")){
                            ew.eq("STATE",sysUser.getState());
                        }
                    }
                }
            }
        }
        List<SysUser> userList = this.baseMapper.selectList(ew);
        return new PageInfo<>(userList);
    }

    //重写用户新增方法
    @Override
    public boolean addUser(SysUser sysUser) {
        //1.生成加密盐值
        String salt = RandomStringUtils.randomAlphanumeric(20);
        //2.替换密码
        sysUser.setPassword(ShiroUtils.encodeSalt(sysUser.getPassword(),salt));
        sysUser.setSalt(salt);
        //3.保存操作人信息
        sysUser.setCreateUserid(ShiroUtils.getUserId());
        sysUser.setUpdateUserid(ShiroUtils.getUserId());
        this.baseMapper.insert(sysUser);
        //4.保存用户角色列表
        if (sysUser.getRoleIdList().size()>0){
            sysUserRoleService.addSysUserRole(sysUser);
        }
        //5.保存用户
        return true;
//        return this.baseMapper.insert(sysUser) > 0 ? true : false;
    }

    //重写用户编辑方法
    @Override
    public boolean updateUser(SysUser sysUser) {
        sysUser.setUpdateUserid(ShiroUtils.getUserId());
        sysUser.setUpdateDate(new Date());

        //删除人员对应的角色
        EntityWrapper<SysUserRole> ew = new EntityWrapper<>();
        ew.eq("USER_ID",sysUser.getId());
        sysUserRoleService.delete(ew);

        //删除后重新添加
        String userId = sysUser.getId();
        List<String> roleIdList = sysUser.getRoleIdList();
        if (roleIdList.size()>0){
            for (String roleId : roleIdList){
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(roleId);
                sysUserRole.setUserId(userId);
                sysUserRole.setUpdateUserid(ShiroUtils.getUserId());
                sysUserRole.setCreateUserid(ShiroUtils.getUserId());
                boolean result = sysUserRoleService.insert(sysUserRole);
            }
        }
        return this.baseMapper.updateById(sysUser) > 0 ? true : false;
    }

    @Override
    public SysUser queryUserByLoginName(String userLoginName) {
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        ew.eq("LOGIN_NAME",userLoginName);

        return sysUserService.selectOne(ew);
    }

    @Override
    public Integer queryUserByLoginNames(String userLoginName) {
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        ew.eq("LOGIN_NAME",userLoginName);
        Integer count = this.baseMapper.queryUserByLoginNames(ew);
        return count;
    }

}
